package org.apache.doris.demo.stream;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestApplication {
   static final String DB_URL = "jdbc:mysql://doris:9030/test_db";
   static final String USER = "root";
   static final String PASS = "000000";

   public static void main(String[] args) throws Exception {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
          Statement stmt = conn.createStatement();
      ) {		      
       String   sql = "\n" +
                  "CREATE TABLE IF NOT EXISTS test_db.expamle_range_tbl\n" +
                  "(\n" +
                  " `user_id` LARGEINT NOT NULL COMMENT \"用户 id\",\n" +
                  " `date` DATE NOT NULL COMMENT \"数据灌入日期时间\",\n" +
                  " `timestamp` DATETIME NOT NULL COMMENT \"数据灌入的时间戳\",\n" +
                  " `city` VARCHAR(20) COMMENT \"用户所在城市\",\n" +
                  " `age` SMALLINT COMMENT \"用户年龄\",\n" +
                  " `sex` TINYINT COMMENT \"用户性别\",\n" +
                  " `last_visit_date` DATETIME REPLACE DEFAULT \"1970-01-01 00:00:00\" COMMENT \"用户最后一次访问时间\",\n" +
                  " `cost` BIGINT SUM DEFAULT \"0\" COMMENT \"用户总消费\",\n" +
                  " `max_dwell_time` INT MAX DEFAULT \"0\" COMMENT \"用户最大停留时间\",\n" +
                  " `min_dwell_time` INT MIN DEFAULT \"99999\" COMMENT \"用户最小停留时间\" )\n" +
                  "ENGINE=olap\n" +
                  "AGGREGATE KEY(`user_id`, `date`, `timestamp`, `city`, `age`, `sex`)\n" +
                  "PARTITION BY RANGE(`date`)\n" +
                  "(\n" +
                  " PARTITION `p201701` VALUES LESS THAN (\"2017-02-01\"),\n" +
                  " PARTITION `p201702` VALUES LESS THAN (\"2017-03-01\"),\n" +
                  " PARTITION `p201703` VALUES LESS THAN (\"2017-04-01\")\n" +
                  ")\n" +
                  "DISTRIBUTED BY HASH(`user_id`) BUCKETS 16\n" +
                  "PROPERTIES\n" +
                  "(\n" +
                  " \"replication_num\" = \"1\",\n" +
                  " \"storage_cooldown_time\" = \"2023-08-01 12:00:00\"\n" +
                  ");";

         stmt.executeUpdate(sql);
         System.out.println("Created table in given database...");   	  
      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}